

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Keras Exporter &mdash; Nyoka 4.2.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript">
          var DOCUMENTATION_OPTIONS = {
              URL_ROOT:'../',
              VERSION:'4.2.0',
              LANGUAGE:'None',
              COLLAPSE_INDEX:false,
              FILE_SUFFIX:'.html',
              HAS_SOURCE:  true,
              SOURCELINK_SUFFIX: '.txt'
          };
      </script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> Nyoka
          

          
          </a>

          
            
            
              <div class="version">
                4.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="../statsmodels_to_pmml.html">Statsmodels Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../keras_model_to_pmml.html">Keras Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../retinanet.html">RetinaNet Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../lgb_to_pmml.html">LightGBM Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../pre_process.html">Pre-Processing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../skl_to_pmml.html">Scikit-Learn Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../xgboost_to_pmml.html">XGBoost Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../exponential_smoothing.html">ExponentialSmoothing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../preprocess_nyoka.html">Nyoka's Pre-Processing Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../enums.html">Enums Module</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Nyoka</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>Keras Exporter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for Keras Exporter</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>

<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
<span class="n">BASE_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">json</span>

<span class="kn">import</span> <span class="nn">PMML44</span> <span class="k">as</span> <span class="nn">ny</span>
<span class="kn">import</span> <span class="nn">metadata</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">base64</span>

<span class="n">warnings</span><span class="o">.</span><span class="n">formatwarning</span> <span class="o">=</span> <span class="n">warnings</span><span class="o">.</span><span class="n">formatwarning</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">msg</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>

<span class="n">KERAS_LAYER_TYPES_MAP</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;InputLayer&#39;</span><span class="p">:</span> <span class="s1">&#39;Input&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Add&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Concatenate&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Dot&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Subtract&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Maximum&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Minimum&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;Average&#39;</span><span class="p">:</span> <span class="s1">&#39;MergeLayer&#39;</span><span class="p">}</span>

<span class="n">KERAS_LAYER_PARAMS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;filters&#39;</span><span class="p">,</span> <span class="s1">&#39;kernel_size&#39;</span><span class="p">,</span> <span class="s1">&#39;strides&#39;</span><span class="p">,</span> <span class="s1">&#39;padding&#39;</span><span class="p">,</span>
                      <span class="s1">&#39;input_shape&#39;</span><span class="p">,</span> <span class="s1">&#39;output_shape&#39;</span><span class="p">,</span> <span class="s2">&quot;activation&quot;</span><span class="p">,</span> <span class="s2">&quot;axis&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;epsilon&quot;</span><span class="p">,</span> <span class="s2">&quot;pool_size&quot;</span><span class="p">,</span> <span class="s2">&quot;scale&quot;</span><span class="p">,</span> <span class="s2">&quot;center&quot;</span><span class="p">,</span> <span class="s2">&quot;depth_multiplier&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;rate&quot;</span><span class="p">,</span> <span class="s2">&quot;dilation_rate&quot;</span><span class="p">,</span><span class="s2">&quot;size&quot;</span><span class="p">,</span><span class="s2">&quot;stride&quot;</span><span class="p">,</span><span class="s2">&quot;ratios&quot;</span><span class="p">,</span><span class="s2">&quot;scales&quot;</span><span class="p">,</span><span class="s2">&quot;mean&quot;</span><span class="p">,</span><span class="s2">&quot;std&quot;</span><span class="p">,</span><span class="s2">&quot;nms_threshold&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;score_threshold&quot;</span><span class="p">]</span>

<span class="n">NYOKA_LAYER_PARAMS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;featureMaps&#39;</span><span class="p">,</span> <span class="s1">&#39;kernel&#39;</span><span class="p">,</span> <span class="s1">&#39;stride&#39;</span><span class="p">,</span> <span class="s1">&#39;pad&#39;</span><span class="p">,</span>
                      <span class="s1">&#39;inputDimension&#39;</span><span class="p">,</span> <span class="s1">&#39;outputDimension&#39;</span><span class="p">,</span>
                      <span class="s2">&quot;activationFunction&quot;</span><span class="p">,</span> <span class="s2">&quot;axis&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;batchNormalizationEpsilon&quot;</span><span class="p">,</span> <span class="s2">&quot;poolSize&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;batchNormalizationScale&quot;</span><span class="p">,</span> <span class="s2">&quot;batchNormalizationCenter&quot;</span><span class="p">,</span> <span class="s2">&quot;depthMultiplier&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;dropoutRate&quot;</span><span class="p">,</span> <span class="s2">&quot;dilationRate&quot;</span><span class="p">,</span><span class="s2">&quot;upsamplingSize&quot;</span><span class="p">,</span><span class="s2">&quot;anchorStride&quot;</span><span class="p">,</span><span class="s2">&quot;anchorRatios&quot;</span><span class="p">,</span><span class="s2">&quot;anchorScales&quot;</span><span class="p">,</span>
                      <span class="s2">&quot;regressBoxesMean&quot;</span><span class="p">,</span> <span class="s2">&quot;regressBoxesStd&quot;</span><span class="p">,</span><span class="s2">&quot;nmsThreshold&quot;</span><span class="p">,</span><span class="s2">&quot;scoreThreshold&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="KerasHeader"><span class="k">class</span> <span class="nc">KerasHeader</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">Header</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates header for Keras PMML model file using Nyoka</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    description : String</span>
<span class="sd">        Description of the PMML file provided as a default</span>
<span class="sd">    copyright : String</span>
<span class="sd">        Adds the information about the copyright.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka header object</span>
<span class="sd">    &quot;&quot;&quot;</span> 
 
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">description</span><span class="p">,</span> <span class="n">copyright</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">description</span><span class="p">:</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s2">&quot;Keras Model in PMML&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">copyright</span><span class="p">:</span>
            <span class="n">copyright</span> <span class="o">=</span> <span class="s2">&quot;Copyright (c) 2018 Software AG&quot;</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">Header</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">copyright</span><span class="o">=</span><span class="n">copyright</span><span class="p">,</span>
                           <span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">,</span>
                           <span class="n">Timestamp</span><span class="o">=</span><span class="n">ny</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())),</span>
                           <span class="n">Application</span><span class="o">=</span><span class="n">ny</span><span class="o">.</span><span class="n">Application</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Nyoka&quot;</span><span class="p">,</span><span class="n">version</span><span class="o">=</span><span class="n">metadata</span><span class="o">.</span><span class="n">__version__</span><span class="p">))</span></div>


<div class="viewcode-block" id="KerasNetworkLayer"><span class="k">class</span> <span class="nc">KerasNetworkLayer</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates Networklayer of PMML which has information about the layer type, weight matrix and bias matrix and their properties.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    layer : Keras layer object</span>
<span class="sd">        Keras layer object</span>
<span class="sd">    dataSet : String</span>
<span class="sd">        Name of the dataset</span>
<span class="sd">    layer_type : String</span>
<span class="sd">        Class name of the layer</span>
<span class="sd">    script_args : Dictionary or None</span>
<span class="sd">        Parameters for the script if any preprocessing script is provided</span>
<span class="sd">    connection_layer_id : boolean</span>
<span class="sd">        Whether to generate connection layer IDs or not</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka NetworkLayer object</span>
<span class="sd">    &quot;&quot;&quot;</span> 

    <span class="k">def</span> <span class="nf">_get_flatten_weights</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">weights</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Flattens the input</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        weights : array</span>
<span class="sd">            Array of weights</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        flat_weights : array</span>
<span class="sd">            Flattened input array in Base64String format</span>

<span class="sd">        weights_shape : array</span>
<span class="sd">            Shape of the flattened array</span>
<span class="sd">        &quot;&quot;&quot;</span> 
        <span class="n">flat_weights</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">weights_shape</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">weights</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">weights</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">weights</span><span class="p">:</span>
                <span class="n">weights_shape</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
                <span class="n">flat_weights</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">flatten</span><span class="p">())</span>
            <span class="n">flat_weights</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">flat_weights</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">weights</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">weights_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">weights_shape</span><span class="p">),</span> <span class="n">item</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">weights_shape</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">weights_shape</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">flat_weights</span><span class="p">,</span> <span class="n">weights_shape</span>

    <span class="k">def</span> <span class="nf">_get_enumerated_input_shape</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the input shape from the Keras Input layer </span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        input_shape : array</span>
<span class="sd">            Array of shape</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        input_dims : array</span>
<span class="sd">            Array of shape for Nyoka PMML</span>
<span class="sd">        &quot;&quot;&quot;</span> 
        <span class="n">input_dims</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="n">in_s</span> <span class="o">=</span> <span class="n">input_shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">in_s</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">input_dims</span> <span class="o">=</span> <span class="nb">str</span><span class="p">((</span><span class="n">in_s</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">input_dims</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">in_s</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">new_shape_lst</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i_shape</span> <span class="ow">in</span> <span class="n">input_shape</span><span class="p">:</span>
                <span class="n">new_shape_lst</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">i_shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])))</span>
            <span class="n">input_dims</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">new_shape_lst</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">input_dims</span>

    <span class="k">def</span> <span class="nf">_get_activation_function</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">layer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Identifies the Activation Function from a given Keras layer</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        layer : Keras layer object</span>
<span class="sd">            A Keras Layer</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        activation_function : String</span>
<span class="sd">            Activation function of the given Keras layer</span>

<span class="sd">        &quot;&quot;&quot;</span> 
        <span class="n">layer_config</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">get_config</span><span class="p">()</span>
        <span class="k">if</span> <span class="s1">&#39;activation&#39;</span> <span class="ow">in</span> <span class="n">layer_config</span><span class="p">:</span>
            <span class="n">activation_function</span> <span class="o">=</span> <span class="n">layer_config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;activation&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">activation_function</span> <span class="o">==</span> <span class="s2">&quot;relu&quot;</span><span class="p">:</span>
                <span class="n">activation_function</span> <span class="o">=</span> <span class="s2">&quot;rectifier&quot;</span>
            <span class="k">elif</span> <span class="n">activation_function</span> <span class="o">==</span> <span class="s2">&quot;relu6&quot;</span><span class="p">:</span>
                <span class="n">activation_function</span> <span class="o">=</span> <span class="s2">&quot;reLU6&quot;</span>
            <span class="k">elif</span> <span class="n">activation_function</span> <span class="o">==</span> <span class="s2">&quot;tanh&quot;</span><span class="p">:</span>
                <span class="n">activation_function</span> <span class="o">=</span> <span class="s2">&quot;tanch&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">activation_function</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">activation_function</span>

    <span class="k">def</span> <span class="nf">_get_layer_params_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">layer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pull out the relevant Nyoka layer attributes matching with Keras layer attributes and pulls values for the respective attributes</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        layer : Keras layer object</span>
<span class="sd">            A Keras Layer</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        new_layer_params_dict : Dictionary</span>
<span class="sd">            Nyoka Layer attributes in a dictionary format</span>

<span class="sd">        &quot;&quot;&quot;</span> 
        <span class="n">layer_params_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">NYOKA_LAYER_PARAMS</span><span class="p">,</span> <span class="n">KERAS_LAYER_PARAMS</span><span class="p">))</span>
        <span class="n">layer_config</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">get_config</span><span class="p">()</span>
        <span class="n">new_layer_params_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">pad_dims</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">layer_params_dict</span><span class="p">[</span><span class="s1">&#39;paddingDims&#39;</span><span class="p">]</span><span class="o">=</span><span class="s1">&#39;None&#39;</span>  
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">layer_params_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">layer_config</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">val</span> <span class="o">==</span> <span class="s2">&quot;activation&quot;</span><span class="p">:</span>
                    <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_activation_function</span><span class="p">(</span><span class="n">layer</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">val</span> <span class="o">==</span> <span class="s2">&quot;padding&quot;</span><span class="p">:</span>
                    <span class="n">pad_val</span> <span class="o">=</span> <span class="n">layer_config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">pad_val</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;valid&#39;</span><span class="p">,</span><span class="s1">&#39;same&#39;</span><span class="p">]:</span>
                        <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pad_val</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">pad_dims</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">pad_val</span><span class="p">)</span>
                        <span class="n">layer_params_dict</span><span class="p">[</span><span class="s1">&#39;paddingDims&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pad_dims</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">layer_config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">val</span><span class="p">)))</span>\
                         <span class="k">if</span> <span class="n">layer_config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;list&#39;</span> <span class="k">else</span> <span class="nb">str</span><span class="p">(</span><span class="n">layer_config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">val</span><span class="p">))</span>
            <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">val</span> <span class="o">==</span> <span class="s2">&quot;input_shape&quot;</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">shape</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
                    <span class="k">except</span><span class="p">:</span>
                        <span class="n">shape</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">get_input_shape_at</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> 
                    <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_enumerated_input_shape</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">val</span> <span class="o">==</span> <span class="s2">&quot;output_shape&quot;</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">shape</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
                    <span class="k">except</span><span class="p">:</span>
                        <span class="n">shape</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">get_output_shape_at</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> 
                    <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_enumerated_input_shape</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span><span class="n">val</span><span class="p">)))</span>\
                         <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span><span class="n">val</span><span class="p">)</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;list&#39;</span> <span class="k">else</span> <span class="nb">str</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span><span class="n">val</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="ow">and</span> <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">!=</span> <span class="s2">&quot;None&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s2">&quot;pad&quot;</span> <span class="ow">and</span> <span class="n">pad_dims</span><span class="p">:</span>
                    <span class="n">pad_</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">pad_val</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">val</span><span class="p">,</span><span class="s1">&#39;__len__&#39;</span><span class="p">):</span>
                            <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">val</span><span class="p">:</span>
                                <span class="n">pad_</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">pad_</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
                    <span class="n">pad_val</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pad_</span><span class="p">)</span>
                    <span class="n">new_layer_params_dict</span><span class="p">[</span><span class="s2">&quot;paddingDims&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pad_val</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">new_layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">new_layer_params_dict</span>

    <span class="k">def</span> <span class="nf">_get_layer_weights_n_biases</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">layer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pulls out the Weights and Bias matrix from a given Keras layer</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        layer : Keras layer object</span>
<span class="sd">            A Keras Layer</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        layer_weights : array</span>
<span class="sd">            Weights of the Keras layer in Base64String format</span>
<span class="sd">        layer_biases : array</span>
<span class="sd">            Bias of the Keras layer in Base64String format</span>

<span class="sd">        &quot;&quot;&quot;</span> 
        <span class="n">layer_all_weights</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">get_weights</span><span class="p">()</span>
        <span class="n">layer_weights</span> <span class="o">=</span> <span class="n">layer_biases</span> <span class="o">=</span> <span class="n">biases</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">layer_all_weights</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span> <span class="s1">&#39;use_bias&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">layer</span><span class="o">.</span><span class="n">use_bias</span><span class="p">:</span>
                <span class="n">biases</span> <span class="o">=</span> <span class="n">layer_all_weights</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">weights</span><span class="p">,</span> <span class="n">w_shape</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_flatten_weights</span><span class="p">(</span>
                    <span class="n">layer_all_weights</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">layer_weights</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">LayerWeights</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">weights</span><span class="p">,</span>
                                                <span class="n">floatsPerLine</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                                                <span class="n">weightsShape</span><span class="o">=</span><span class="n">w_shape</span><span class="p">,</span>
                                                <span class="n">weightsFlattenAxis</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">weights</span><span class="p">,</span> <span class="n">w_shape</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_flatten_weights</span><span class="p">(</span><span class="n">layer_all_weights</span><span class="p">)</span>
                <span class="n">layer_weights</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">LayerWeights</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">weights</span><span class="p">,</span>
                                                <span class="n">floatsPerLine</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                                                <span class="n">weightsShape</span><span class="o">=</span><span class="n">w_shape</span><span class="p">,</span>
                                                <span class="n">weightsFlattenAxis</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">biases</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">bs_shape</span> <span class="o">=</span> <span class="n">biases</span><span class="o">.</span><span class="n">shape</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">bs_shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">final_bs_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">((</span><span class="n">bs_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">final_bs_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">bs_shape</span><span class="p">)</span>
                <span class="n">layer_biases</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">LayerBias</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">biases</span><span class="p">,</span>
                                            <span class="n">biasShape</span><span class="o">=</span><span class="n">final_bs_shape</span><span class="p">,</span>
                                            <span class="n">biasFlattenAxis</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">,</span>
                                            <span class="n">floatsPerLine</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">layer_weights</span><span class="p">,</span> <span class="n">layer_biases</span>

    <span class="k">def</span> <span class="nf">_get_connection_layer_ids</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">layer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pulls out the Connection ID of the Keras layer</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        layer : Keras layer object</span>
<span class="sd">            A Keras Layer</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        connection_layers : String</span>
<span class="sd">            The Layer ID of the Keras Layer</span>

<span class="sd">        &quot;&quot;&quot;</span> 
        <span class="n">node_config</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">_inbound_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get_config</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">node_config</span><span class="p">[</span><span class="s1">&#39;inbound_layers&#39;</span><span class="p">]:</span>
            <span class="n">inbound_layers</span> <span class="o">=</span> <span class="n">node_config</span><span class="p">[</span><span class="s1">&#39;inbound_layers&#39;</span><span class="p">]</span>
            <span class="n">connection_layers</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">inbound_layers</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">connection_layers</span> <span class="o">=</span> <span class="s2">&quot;na&quot;</span>
        <span class="k">return</span> <span class="n">connection_layers</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">layer</span><span class="p">,</span><span class="n">dataSet</span><span class="p">,</span> <span class="n">layer_type</span><span class="p">,</span> <span class="n">script_args</span><span class="p">,</span> <span class="n">connection_layer_id</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="n">merge_layer_op_type</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">merge_concat_axes</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">merge_dot_axes</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">merge_dot_normalization</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">connection_layers</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">input_field_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="s2">&quot;Pmml&quot;</span> <span class="ow">in</span> <span class="n">layer_type</span><span class="p">:</span>
            <span class="n">layer_type</span> <span class="o">=</span> <span class="n">layer_type</span><span class="p">[</span><span class="mi">4</span><span class="p">:]</span>
        <span class="n">old_layer_type</span> <span class="o">=</span> <span class="n">layer_type</span>
        <span class="n">layer_type</span> <span class="o">=</span> <span class="n">KERAS_LAYER_TYPES_MAP</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">layer_type</span><span class="p">,</span> <span class="n">layer_type</span><span class="p">)</span>
        <span class="n">layer_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_layer_params_dict</span><span class="p">(</span><span class="n">layer</span><span class="p">)</span>
        <span class="n">layer_weights</span><span class="p">,</span> <span class="n">layer_biases</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_layer_weights_n_biases</span><span class="p">(</span><span class="n">layer</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">connection_layer_id</span><span class="p">:</span>
            <span class="n">connection_layers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_connection_layer_ids</span><span class="p">(</span><span class="n">layer</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s2">&quot;MergeLayer&quot;</span><span class="p">:</span>
            <span class="n">merge_layer_op_type</span> <span class="o">=</span> <span class="n">old_layer_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">merge_layer_op_type</span> <span class="o">==</span> <span class="s2">&quot;concatenate&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="s2">&quot;axis&quot;</span> <span class="ow">in</span> <span class="n">layer_params</span><span class="p">:</span>
                    <span class="n">merge_concat_axes</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;axis&quot;</span><span class="p">]</span>
                    <span class="k">del</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;axis&quot;</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">merge_layer_op_type</span> <span class="o">==</span> <span class="s1">&#39;dot&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="s1">&#39;axes&#39;</span> <span class="ow">in</span> <span class="n">layer_params</span><span class="p">:</span>
                    <span class="n">merge_dot_axes</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;axes&quot;</span><span class="p">]</span>
                    <span class="k">del</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;axes&quot;</span><span class="p">]</span>
                <span class="k">if</span> <span class="s1">&#39;normalize&#39;</span> <span class="ow">in</span> <span class="n">layer_params</span><span class="p">:</span>
                    <span class="n">merge_dot_normalization</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;normalize&quot;</span><span class="p">]</span>
                    <span class="k">del</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;normalize&quot;</span><span class="p">]</span>
                <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;mergeLayerDotNormalize&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">merge_dot_normalization</span>
        <span class="k">elif</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s2">&quot;BatchNormalization&quot;</span><span class="p">:</span>
            <span class="n">layer_type</span> <span class="o">=</span> <span class="s2">&quot;BatchNormalization&quot;</span>
            <span class="k">if</span> <span class="s2">&quot;axis&quot;</span> <span class="ow">in</span> <span class="n">layer_params</span><span class="p">:</span>
                <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;batchNormalizationAxis&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;axis&quot;</span><span class="p">]</span>
                <span class="k">del</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;axis&quot;</span><span class="p">]</span>
            <span class="k">if</span> <span class="s2">&quot;batchNormalizationScale&quot;</span> <span class="ow">in</span> <span class="n">layer_params</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;batchNormalizationScale&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">layer_params</span><span class="p">[</span>
                        <span class="s2">&quot;batchNormalizationScale&quot;</span><span class="p">])</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;batchNormalizationScale&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span>
                        <span class="s2">&quot;batchNormalizationScale&quot;</span><span class="p">]</span>
            <span class="k">if</span> <span class="s2">&quot;batchNormalizationCenter&quot;</span> <span class="ow">in</span> <span class="n">layer_params</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;batchNormalizationCenter&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">layer_params</span><span class="p">[</span>
                        <span class="s2">&quot;batchNormalizationCenter&quot;</span><span class="p">])</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;batchNormalizationCenter&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span>
                        <span class="s2">&quot;batchNormalizationCenter&quot;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s2">&quot;ReLU&quot;</span><span class="p">:</span>
            <span class="n">layer_type</span> <span class="o">=</span> <span class="s2">&quot;Activation&quot;</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;activationFunction&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;reLU6&quot;</span>
        <span class="k">elif</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s2">&quot;Anchors&quot;</span><span class="p">:</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;anchorSize&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;upsamplingSize&quot;</span><span class="p">]</span>
            <span class="k">del</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;upsamplingSize&quot;</span><span class="p">]</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;anchorRatios&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;anchorRatios&quot;</span><span class="p">]</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;anchorScales&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;anchorScales&quot;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s2">&quot;RegressBoxes&quot;</span><span class="p">:</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;regressBoxesMean&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;regressBoxesMean&quot;</span><span class="p">]</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;regressBoxesStd&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;regressBoxesStd&quot;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s1">&#39;FilterDetections&#39;</span><span class="p">:</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s1">&#39;nms&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">nms</span>
            <span class="n">layer_params</span><span class="p">[</span><span class="s1">&#39;classSpecificFilter&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">class_specific_filter</span>
        <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;mergeLayerOp&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">merge_layer_op_type</span>
        <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;mergeLayerConcatOperationAxes&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">merge_concat_axes</span>
        <span class="n">layer_params</span><span class="p">[</span><span class="s2">&quot;mergeLayerDotOperationAxis&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">merge_dot_axes</span>
        <span class="k">if</span> <span class="n">layer_type</span> <span class="o">==</span> <span class="s2">&quot;Input&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">dataSet</span><span class="o">==</span><span class="s1">&#39;image&#39;</span> <span class="ow">or</span> <span class="n">script_args</span><span class="p">:</span>
                <span class="n">input_field_name</span> <span class="o">=</span> <span class="s1">&#39;base64String&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">input_field_name</span> <span class="o">=</span> <span class="n">dataSet</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inputFieldName</span><span class="o">=</span><span class="n">input_field_name</span><span class="p">,</span>
                                 <span class="n">layerType</span><span class="o">=</span><span class="n">layer_type</span><span class="p">,</span>
                                 <span class="n">connectionLayerId</span><span class="o">=</span><span class="n">connection_layers</span><span class="p">,</span>
                                 <span class="n">layerId</span><span class="o">=</span><span class="n">layer</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                                 <span class="n">normalizationMethod</span><span class="o">=</span><span class="s2">&quot;none&quot;</span><span class="p">,</span>
                                 <span class="n">LayerParameters</span><span class="o">=</span><span class="n">ny</span><span class="o">.</span><span class="n">LayerParameters</span><span class="p">(</span>
                                     <span class="o">**</span><span class="n">layer_params</span><span class="p">),</span>
                                 <span class="n">LayerWeights</span><span class="o">=</span><span class="n">layer_weights</span><span class="p">,</span>
                                 <span class="n">LayerBias</span><span class="o">=</span><span class="n">layer_biases</span><span class="p">)</span></div>


<div class="viewcode-block" id="KerasDataDictionary"><span class="k">class</span> <span class="nc">KerasDataDictionary</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">DataDictionary</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasDataDictionary stores the class information to be predicted  in the PMML model.</span>
<span class="sd">    The current implementation takes care of the image class label by giving dataset name as dataSet parameter.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    dataSet : String</span>
<span class="sd">        Name of the dataset</span>
<span class="sd">    predictedClasses : List</span>
<span class="sd">        List of class names or values to be predicted.</span>
<span class="sd">    script_args : Dictionary or None</span>
<span class="sd">        Parameters for the script if any preprocessing script is provided</span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka&#39;s DataDictionary Object</span>
<span class="sd">    &quot;&quot;&quot;</span> 
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="p">,</span> <span class="n">script_args</span><span class="p">):</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">DataDictionary</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">predictedClasses</span><span class="p">:</span>
            <span class="n">class_node</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;labels&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">,</span>
                                    <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">predictedClasses</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">pC</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span> <span class="k">for</span> <span class="n">pC</span> <span class="ow">in</span> <span class="n">predictedClasses</span><span class="p">):</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s2">&quot;Not all classes are given as String. Values will be attempted to be converted to String.&quot;</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">predictedClasses</span><span class="p">)):</span>
                    <span class="n">data_val</span> <span class="o">=</span> <span class="n">predictedClasses</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                    <span class="n">class_node</span><span class="o">.</span><span class="n">add_Value</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">Value</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">data_val</span><span class="p">)))</span>
            <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">predictedClasses</span><span class="p">)</span> <span class="o">==</span> <span class="nb">dict</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">pC</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span> <span class="k">for</span> <span class="n">pC</span> <span class="ow">in</span> <span class="n">predictedClasses</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s2">&quot;Class indices are expected as strings in dictionary keys. Keys will be attempted to be converted to String.&quot;</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">predictedClasses</span><span class="o">.</span><span class="n">keys</span><span class="p">())):</span>
                    <span class="n">data_val</span> <span class="o">=</span> <span class="n">predictedClasses</span><span class="o">.</span><span class="n">keys</span><span class="p">()[</span><span class="n">i</span><span class="p">]</span>
                    <span class="n">class_node</span><span class="o">.</span><span class="n">add_Value</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">Value</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">data_val</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">class_node</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;predictions&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;continuous&quot;</span><span class="p">,</span>
                                        <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;double&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dataSet</span> <span class="o">==</span> <span class="s2">&quot;image&quot;</span> <span class="ow">or</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">dataSet</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">DataDictionary</span><span class="o">.</span><span class="n">add_DataField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;binary&quot;</span><span class="p">,</span>
                <span class="n">mimeType</span><span class="o">=</span><span class="s2">&quot;image/png&quot;</span><span class="p">,</span> <span class="n">Extension</span><span class="o">=</span><span class="p">[</span><span class="n">ny</span><span class="o">.</span><span class="n">Extension</span><span class="p">(</span>
                    <span class="n">extender</span><span class="o">=</span><span class="s2">&quot;ADAPA&quot;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;BINARY_BUFFERED&quot;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s2">&quot;true&quot;</span><span class="p">)]))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">dataSet</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">DataDictionary</span><span class="o">.</span><span class="n">add_DataField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span>
            <span class="p">))</span>           

        <span class="n">ny</span><span class="o">.</span><span class="n">DataDictionary</span><span class="o">.</span><span class="n">add_DataField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">class_node</span><span class="p">)</span></div>


<div class="viewcode-block" id="KerasMiningSchema"><span class="k">class</span> <span class="nc">KerasMiningSchema</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasMiningSchema stores the attributes which are used to build the model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    dataSet : String</span>
<span class="sd">        Name of the dataset</span>
<span class="sd">    predictedClasses : List</span>
<span class="sd">        List of class names or values to be predicted.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka&#39;s MiningSchema Object</span>
<span class="sd">    &quot;&quot;&quot;</span> 
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="p">):</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">MiningSchema</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">dataSet</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">MiningSchema</span><span class="o">.</span><span class="n">add_MiningField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="n">usageType</span><span class="o">=</span><span class="s2">&quot;active&quot;</span><span class="p">,</span>
            <span class="n">invalidValueTreatment</span><span class="o">=</span><span class="s2">&quot;asIs&quot;</span><span class="p">))</span>

        <span class="n">ny</span><span class="o">.</span><span class="n">MiningSchema</span><span class="o">.</span><span class="n">add_MiningField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="s2">&quot;labels&quot;</span> <span class="k">if</span> <span class="n">predictedClasses</span> <span class="k">else</span> <span class="s2">&quot;predictions&quot;</span><span class="p">,</span> <span class="n">usageType</span><span class="o">=</span><span class="s2">&quot;target&quot;</span><span class="p">,</span>
            <span class="n">invalidValueTreatment</span><span class="o">=</span><span class="s2">&quot;asIs&quot;</span><span class="p">))</span></div>


<div class="viewcode-block" id="KerasOutput"><span class="k">class</span> <span class="nc">KerasOutput</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">Output</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasOutput provides the information about the output representation of the PMML. (e.g. Predicted classes, probabilities)</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    predictedClasses : List or None</span>
<span class="sd">        List of Classes for which model has been trained. If not provided, considered as Regression</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka&#39;s Output Object</span>
<span class="sd">    &quot;&quot;&quot;</span> 
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">Output</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">predictedClasses</span><span class="p">:</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">Output</span><span class="o">.</span><span class="n">add_OutputField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;predicted_label&quot;</span><span class="p">,</span> <span class="n">feature</span><span class="o">=</span><span class="s2">&quot;predictedValue&quot;</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">))</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">Output</span><span class="o">.</span><span class="n">add_OutputField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;top1_prob&quot;</span><span class="p">,</span> <span class="n">feature</span><span class="o">=</span><span class="s2">&quot;probability&quot;</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;double&quot;</span><span class="p">))</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">Output</span><span class="o">.</span><span class="n">add_OutputField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;top5_prob&quot;</span><span class="p">,</span> <span class="n">feature</span><span class="o">=</span><span class="s2">&quot;topCategories&quot;</span><span class="p">,</span> <span class="n">numTopCategories</span><span class="o">=</span><span class="s2">&quot;5&quot;</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">Output</span><span class="o">.</span><span class="n">add_OutputField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;predicted_predictions&quot;</span><span class="p">,</span> <span class="n">feature</span><span class="o">=</span><span class="s2">&quot;predictedValue&quot;</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;double&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;continuous&quot;</span><span class="p">))</span></div>



<div class="viewcode-block" id="KerasLocalTransformations"><span class="k">class</span> <span class="nc">KerasLocalTransformations</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">LocalTransformations</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasLocalTransformations provides the information about the list of transformations applied to the data.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    keras_model : Keras model object</span>
<span class="sd">        Keras model object</span>
<span class="sd">    dataSet : String</span>
<span class="sd">        Name of the dataset</span>
<span class="sd">    script_args : Dictionary</span>
<span class="sd">        Parameters for the script if any preprocessing script is provided</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka&#39;s LocalTransformations Object</span>
<span class="sd">    &quot;&quot;&quot;</span> 
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keras_model</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">):</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">LocalTransformations</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="n">ret_type</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;return_type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="n">def_name</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;def_name&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="o">==</span><span class="s1">&#39;str&#39;</span> <span class="k">else</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span><span class="o">.</span><span class="vm">__name__</span>

            <span class="n">ny</span><span class="o">.</span><span class="n">LocalTransformations</span><span class="o">.</span><span class="n">add_DerivedField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">DerivedField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;base64String&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span> <span class="k">if</span> <span class="n">ret_type</span><span class="o">==</span><span class="s2">&quot;string&quot;</span> <span class="k">else</span> <span class="s2">&quot;continuous&quot;</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="n">ret_type</span><span class="p">,</span> <span class="n">Apply</span><span class="o">=</span><span class="n">ny</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span><span class="n">function</span><span class="o">=</span><span class="s1">&#39;customFunc&#39;</span><span class="p">,</span>
                <span class="n">FieldRef</span><span class="o">=</span><span class="p">[</span><span class="n">ny</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">dataSet</span><span class="p">)])</span>
            <span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ny</span><span class="o">.</span><span class="n">LocalTransformations</span><span class="o">.</span><span class="n">add_DerivedField</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ny</span><span class="o">.</span><span class="n">DerivedField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;base64String&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span>
                <span class="n">Apply</span><span class="o">=</span><span class="n">ny</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span><span class="n">function</span><span class="o">=</span><span class="s2">&quot;CNN:getBase64String&quot;</span><span class="p">,</span>
                            <span class="n">FieldRef</span><span class="o">=</span><span class="p">[</span><span class="n">ny</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="s2">&quot;image&quot;</span><span class="p">)])))</span></div>


<div class="viewcode-block" id="KerasTransformationDictionary"><span class="k">class</span> <span class="nc">KerasTransformationDictionary</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">TransformationDictionary</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasTransformationDictionary provides the information about the list of transformations functions applied to the data.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    dataSet : string</span>
<span class="sd">        name of the input</span>
<span class="sd">    script_args : Dictionary</span>
<span class="sd">        Parameters for the script if any preprocessing script is provided</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka&#39;s TransformationDictionary object </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;str&#39;</span><span class="p">:</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span>
            <span class="n">def_name</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;def_name&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="kn">import</span> <span class="nn">inspect</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">])</span>
            <span class="n">def_name</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="n">encode</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="s2">&quot;encode&quot;</span> <span class="ow">in</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="n">encode</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;encode&#39;</span><span class="p">]</span>
        
        <span class="k">if</span> <span class="n">encode</span><span class="p">:</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">content</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span>
        <span class="n">return_type</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;return_type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="n">extension</span> <span class="o">=</span> <span class="p">[</span><span class="n">ny</span><span class="o">.</span><span class="n">Extension</span><span class="p">(</span>
            <span class="n">extender</span><span class="o">=</span><span class="s1">&#39;ADAPA&#39;</span><span class="p">,</span> 
            <span class="n">name</span><span class="o">=</span><span class="n">def_name</span><span class="p">,</span> 
            <span class="n">value</span><span class="o">=</span><span class="n">return_type</span><span class="p">,</span>
            <span class="n">anytypeobjs_</span><span class="o">=</span><span class="p">[</span><span class="n">content</span><span class="p">]</span>
            <span class="p">)]</span>
        <span class="n">def_func</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">DefineFunction</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="s1">&#39;customFunc&#39;</span><span class="p">,</span>
            <span class="n">optype</span><span class="o">=</span><span class="s1">&#39;categorical&#39;</span> <span class="k">if</span> <span class="n">return_type</span> <span class="o">==</span> <span class="s1">&#39;string&#39;</span> <span class="k">else</span> <span class="s1">&#39;continous&#39;</span><span class="p">,</span>
            <span class="n">dataType</span><span class="o">=</span><span class="n">return_type</span><span class="p">,</span>
            <span class="n">ParameterField</span><span class="o">=</span><span class="p">[</span><span class="n">ny</span><span class="o">.</span><span class="n">ParameterField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">dataSet</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="s1">&#39;binary&#39;</span><span class="p">)],</span>
            <span class="n">Apply</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span><span class="n">function</span><span class="o">=</span><span class="s1">&#39;python&#39;</span><span class="p">,</span> <span class="n">Extension</span><span class="o">=</span><span class="n">extension</span><span class="p">,</span> <span class="n">FieldRef</span><span class="o">=</span><span class="p">[</span><span class="n">ny</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">dataSet</span><span class="p">)]),</span>

        <span class="p">)</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">TransformationDictionary</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">TransformationDictionary</span><span class="o">.</span><span class="n">add_DefineFunction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">def_func</span><span class="p">)</span></div>



<div class="viewcode-block" id="KerasNetwork"><span class="k">class</span> <span class="nc">KerasNetwork</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasNetwork creates the DeepNetwork object which stores the NetworkLayer in sequence to define the architecture.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    keras_model : Keras model object</span>
<span class="sd">        Keras model object</span>
<span class="sd">    model_name : String</span>
<span class="sd">        Name of the model</span>
<span class="sd">    dataSet : String</span>
<span class="sd">        Name of the dataset</span>
<span class="sd">    predictedClasses : List or None</span>
<span class="sd">        List of class names</span>
<span class="sd">    script_args : Dictionary or None</span>
<span class="sd">        Parameters for the script if any preprocessing script is provided</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Nyoka&#39;s DeepNetwork Object</span>
<span class="sd">    &quot;&quot;&quot;</span> 


    <span class="k">def</span> <span class="nf">_create_an_input_layer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">layer</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates a PMML input layer from Keras Input Layer object</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        layer : Keras layer object</span>
<span class="sd">            Keras layer object</span>
<span class="sd">        dataSet : String</span>
<span class="sd">            Name of the dataset</span>
<span class="sd">    </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        input_layer: Nyoka Object</span>
<span class="sd">            PMML Input layer object</span>
<span class="sd">        &quot;&quot;&quot;</span>

        
        <span class="k">if</span> <span class="n">dataSet</span><span class="o">==</span><span class="s1">&#39;image&#39;</span> <span class="ow">or</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="n">inputField</span> <span class="o">=</span> <span class="s2">&quot;base64String&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">inputField</span> <span class="o">=</span> <span class="n">dataSet</span>

        <span class="n">in_shape</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">input_shape</span>
        <span class="k">if</span> <span class="n">in_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">in_shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">input_dims</span> <span class="o">=</span> <span class="n">output_dims</span> <span class="o">=</span> <span class="nb">str</span><span class="p">((</span><span class="n">in_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="mi">1</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">input_dims</span> <span class="o">=</span> <span class="n">output_dims</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">in_shape</span><span class="p">)</span>  
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">in_shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">input_dims</span> <span class="o">=</span> <span class="n">output_dims</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">in_shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span><span class="o">+</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">input_dims</span> <span class="o">=</span> <span class="n">output_dims</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">in_shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])))</span>  
        <span class="n">node_config</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">_inbound_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get_config</span><span class="p">()</span>
        <span class="n">connection_layers</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">node_config</span><span class="p">[</span><span class="s1">&#39;inbound_layers&#39;</span><span class="p">])</span>
        <span class="n">input_layer</span> <span class="o">=</span> <span class="n">ny</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">(</span>
            <span class="n">inputFieldName</span><span class="o">=</span><span class="n">inputField</span><span class="p">,</span> <span class="n">layerType</span><span class="o">=</span><span class="s2">&quot;Input&quot;</span><span class="p">,</span> <span class="n">layerId</span><span class="o">=</span><span class="n">connection_layers</span><span class="p">,</span>
            <span class="n">connectionLayerId</span><span class="o">=</span><span class="s2">&quot;na&quot;</span><span class="p">,</span><span class="n">LayerParameters</span><span class="o">=</span><span class="n">ny</span><span class="o">.</span><span class="n">LayerParameters</span><span class="p">(</span>
                <span class="n">inputDimension</span><span class="o">=</span><span class="n">input_dims</span><span class="p">,</span>
                <span class="n">outputDimension</span><span class="o">=</span><span class="n">output_dims</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">input_layer</span>

    <span class="k">def</span> <span class="nf">_create_layers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keras_model</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create list of PMML network layers from Keras Model object.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        keras_model : Keras model object</span>
<span class="sd">            Keras model object</span>
<span class="sd">        dataSet : String</span>
<span class="sd">            Name of the dataset</span>
<span class="sd">    </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        network_layers: Nyoka Object</span>
<span class="sd">            PMML network layer object </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">network_layers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">model_layers</span> <span class="o">=</span> <span class="n">keras_model</span><span class="o">.</span><span class="n">layers</span>
        <span class="n">first_layer</span> <span class="o">=</span> <span class="n">model_layers</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">first_layer</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s2">&quot;InputLayer&quot;</span><span class="p">:</span>
            <span class="n">input_layer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_an_input_layer</span><span class="p">(</span><span class="n">first_layer</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">input_layer</span><span class="p">:</span>
                <span class="n">network_layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">input_layer</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">layer</span> <span class="ow">in</span> <span class="n">model_layers</span><span class="p">:</span>
            <span class="n">layer_type</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="n">net_layer</span> <span class="o">=</span> <span class="n">KerasNetworkLayer</span><span class="p">(</span><span class="n">layer</span><span class="p">,</span><span class="n">dataSet</span><span class="p">,</span> <span class="n">layer_type</span><span class="p">,</span><span class="n">script_args</span><span class="p">)</span>
            <span class="n">network_layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">net_layer</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">network_layers</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keras_model</span><span class="p">,</span> <span class="n">model_name</span><span class="p">,</span> <span class="n">dataSet</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">script_args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">model_name</span><span class="p">:</span>
            <span class="n">model_namme</span> <span class="o">=</span> <span class="n">keras_model</span><span class="o">.</span><span class="n">name</span>
        <span class="n">network_layers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_layers</span><span class="p">(</span><span class="n">keras_model</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">)</span>
        <span class="n">local_trans</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">mining_schema</span> <span class="o">=</span> <span class="n">KerasMiningSchema</span><span class="p">(</span><span class="n">dataSet</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dataSet</span> <span class="o">==</span> <span class="s1">&#39;image&#39;</span> <span class="ow">or</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="n">local_trans</span> <span class="o">=</span> <span class="n">KerasLocalTransformations</span><span class="p">(</span><span class="n">keras_model</span><span class="p">,</span> <span class="n">dataSet</span><span class="p">,</span> <span class="n">script_args</span><span class="p">)</span>
        <span class="n">function_Name</span> <span class="o">=</span> <span class="s2">&quot;classification&quot;</span> <span class="k">if</span> <span class="n">predictedClasses</span> <span class="k">else</span> <span class="s2">&quot;regression&quot;</span>
        <span class="n">ny</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span><span class="p">,</span>
                                <span class="n">functionName</span><span class="o">=</span><span class="n">function_Name</span><span class="p">,</span> <span class="n">algorithmName</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">normalizationMethod</span><span class="o">=</span><span class="s2">&quot;none&quot;</span><span class="p">,</span> <span class="n">numberOfLayers</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">network_layers</span><span class="p">),</span>
                                <span class="n">isScorable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">Extension</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">MiningSchema</span><span class="o">=</span><span class="n">mining_schema</span><span class="p">,</span>
                                <span class="n">Output</span><span class="o">=</span><span class="n">KerasOutput</span><span class="p">(</span><span class="n">predictedClasses</span><span class="p">),</span> <span class="n">LocalTransformations</span><span class="o">=</span><span class="n">local_trans</span><span class="p">,</span>
                                <span class="n">ModelStats</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ModelExplanation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">Targets</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">NetworkLayer</span><span class="o">=</span><span class="n">network_layers</span><span class="p">,</span> <span class="n">NeuralOutputs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">ModelVerification</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span></div>


<div class="viewcode-block" id="KerasToPmml"><span class="k">class</span> <span class="nc">KerasToPmml</span><span class="p">(</span><span class="n">ny</span><span class="o">.</span><span class="n">PMML</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    KerasToPmml exports the Keras model object into PMML file using nyoka.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    keras_model : Keras model object</span>
<span class="sd">        Keras model object </span>
<span class="sd">    model_name : String or None</span>
<span class="sd">        Name to be given to the model in PMML.</span>
<span class="sd">    description : Sting or None</span>
<span class="sd">        Description to be shown in PMML</span>
<span class="sd">    dataSet : String or None</span>
<span class="sd">        Name of the dataset. Value is &#39;image&#39; for Image Classifier, &#39;None&#39; or any other value is for tabular or base64 encoded data. </span>
<span class="sd">    predictedClasses : List or None</span>
<span class="sd">        List of the class names for which model has been trained. If not provided, assumed to be regression model.</span>
<span class="sd">    script_args : Dictionary or None</span>
<span class="sd">        Contains information of the script to be used to convert `image` data into base64 string. Required when dataSet=`image`.</span>
<span class="sd">        Required attributes - </span>
<span class="sd">            content : string or function</span>
<span class="sd">                The content of the script</span>
<span class="sd">            def_name : string</span>
<span class="sd">                name of the function to be used. Required when content is string</span>
<span class="sd">            return_type : string</span>
<span class="sd">                The return type of the function. Valid values are (&#39;string&#39;, &#39;double&#39;, &#39;float&#39;,&#39;integer&#39;)</span>
<span class="sd">            encode : boolean</span>
<span class="sd">                The representation of the script in PMML. If True, the script will be represented as base64 encoded string, else as plain text.</span>
<span class="sd">                If not provided, default value `True` is considered.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Creates Nyoka&#39;s PMML object, this can be saved in file using export function</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">content_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;`content` should be present in script_args, which is either a function or a string (script content)&quot;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">def_name_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;`def_name`, the name of the funciton is required when `content` is a string.&quot;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">ret_type_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;`return_type` of the preprocessing function is required. Valid return types are (&#39;string&#39;, &#39;double&#39;, &#39;float&#39;, &#39;intger&#39;)&quot;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">ret_type_value_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Valid return types are (&#39;string&#39;, &#39;double&#39;, &#39;float&#39;, &#39;intger&#39;)&quot;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">encode_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Valid values for `encode` are (True, False)&quot;</span>
    
<div class="viewcode-block" id="KerasToPmml.validate_script_args">    <span class="k">def</span> <span class="nf">validate_script_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">script_args</span><span class="p">):</span>
        <span class="k">assert</span> <span class="s1">&#39;content&#39;</span> <span class="ow">in</span> <span class="n">script_args</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">content_error</span>
        <span class="k">if</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;str&#39;</span><span class="p">:</span>
            <span class="k">assert</span> <span class="s1">&#39;def_name&#39;</span> <span class="ow">in</span> <span class="n">script_args</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">def_name_error</span>
        <span class="k">assert</span> <span class="s1">&#39;return_type&#39;</span> <span class="ow">in</span> <span class="n">script_args</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ret_type_error</span>
        <span class="n">ret_type</span> <span class="o">=</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;return_type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">assert</span> <span class="n">ret_type</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;string&quot;</span><span class="p">,</span> <span class="s2">&quot;double&quot;</span><span class="p">,</span> <span class="s2">&quot;float&quot;</span><span class="p">,</span> <span class="s2">&quot;intger&quot;</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">ret_type_value_error</span>
        <span class="k">if</span> <span class="s1">&#39;encode&#39;</span> <span class="ow">in</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="k">assert</span> <span class="n">script_args</span><span class="p">[</span><span class="s1">&#39;encode&#39;</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">encode_error</span></div>


    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keras_model</span><span class="p">,</span> <span class="n">model_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">copyright</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>\
        <span class="n">dataSet</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">script_args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">dataSet</span><span class="p">:</span>
            <span class="n">dataSet</span> <span class="o">=</span> <span class="s1">&#39;input&#39;</span>
        <span class="n">data_dict</span> <span class="o">=</span> <span class="n">KerasDataDictionary</span><span class="p">(</span><span class="n">dataSet</span><span class="p">,</span> <span class="n">predictedClasses</span><span class="p">,</span> <span class="n">script_args</span><span class="p">)</span>
        <span class="n">trans_dict</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">script_args</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">validate_script_args</span><span class="p">(</span><span class="n">script_args</span><span class="p">)</span>
            <span class="n">trans_dict</span> <span class="o">=</span> <span class="n">KerasTransformationDictionary</span><span class="p">(</span><span class="n">dataSet</span><span class="p">,</span><span class="n">script_args</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">KerasToPmml</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">version</span><span class="o">=</span><span class="s2">&quot;4.4&quot;</span><span class="p">,</span> <span class="n">Header</span><span class="o">=</span><span class="n">KerasHeader</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">,</span> <span class="n">copyright</span><span class="o">=</span><span class="n">copyright</span><span class="p">),</span>
            <span class="n">DataDictionary</span><span class="o">=</span><span class="n">data_dict</span><span class="p">,</span> <span class="n">TransformationDictionary</span><span class="o">=</span> <span class="n">trans_dict</span><span class="p">,</span> <span class="n">DeepNetwork</span><span class="o">=</span><span class="p">[</span>
                <span class="n">KerasNetwork</span><span class="p">(</span><span class="n">keras_model</span><span class="o">=</span><span class="n">keras_model</span><span class="p">,</span> 
                <span class="n">model_name</span><span class="o">=</span><span class="n">model_name</span><span class="p">,</span> 
                <span class="n">dataSet</span><span class="o">=</span><span class="n">dataSet</span><span class="p">,</span> 
                <span class="n">predictedClasses</span><span class="o">=</span><span class="n">predictedClasses</span><span class="p">,</span>
                <span class="n">script_args</span><span class="o">=</span><span class="n">script_args</span><span class="p">)])</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, maintainer@nyoka.org

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>